package com.bawei.common.widget

import android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet
import android.util.Log
import com.tencent.smtt.export.external.interfaces.ConsoleMessage
import com.tencent.smtt.export.external.interfaces.JsPromptResult
import com.tencent.smtt.export.external.interfaces.JsResult
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient

/**
 * Author: Jason Zhang
 * Date: 2024/5/23
 * Desc: 自定义X5内核的WebView
 */
class X5WebView constructor(context: Context, attrs: AttributeSet): WebView(context, attrs) {

    init {
        //设置客户端
        this.webViewClient = X5WebViewClient()
        this.webChromeClient = X5WebChromeClient()
        //初始化基本配置
        initX5WebView()
    }

    private fun initX5WebView(){
        //设置基本配置
        var webSettings = settings
        webSettings.javaScriptEnabled = true  //是否支持JS脚本
        webSettings.allowFileAccess = true    //是否支持文件访问
        webSettings.loadsImagesAutomatically = true  //是否支持自动加载图片
        webSettings.javaScriptCanOpenWindowsAutomatically = true //是否支持JS弹出窗口
        webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webSettings.setSupportMultipleWindows(false) //是否支持多窗口
        webSettings.useWideViewPort = true //是否支持小窗口
        webSettings.setSupportZoom(false) //是否支持缩放
        webSettings.builtInZoomControls = false //是否显示放大缩小控制按钮
        webSettings.setAppCacheEnabled(true) //是否支持应用缓存
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE //缓存模式
        webSettings.domStorageEnabled = true //是否支持DOM存储
        webSettings.setGeolocationEnabled(false) //是否支持定位
    }

    /**
     * H5页面所有加载流程都可以在当前类中做拦截，
     * 控制H5页面中所有逻辑
     */
    inner class X5WebViewClient: WebViewClient() {

        //H5页面开始加载完成
        override fun onPageStarted(webView: WebView?, url: String?, p2: Bitmap?) {
            super.onPageStarted(webView, url, p2)
        }

        //H5页面关闭了
        override fun onPageFinished(webView: WebView?, url: String?) {
            super.onPageFinished(webView, url)
        }

        //拦截H5页面中URL
        override fun shouldOverrideUrlLoading(webView: WebView?, url: String?): Boolean {
            webView?.loadUrl(url)
            //要返回true否则内核会继续处理
            return true
        }

    }

    /**
     * 控制H5页面中所有与用户交互行为
     */
    inner class X5WebChromeClient: WebChromeClient() {

        override fun onJsAlert(p0: WebView?, p1: String?, p2: String?, p3: JsResult?): Boolean {
            return true
        }

        override fun onJsConfirm(p0: WebView?, p1: String?, p2: String?, p3: JsResult?): Boolean {
            return true
        }

        override fun onJsPrompt(p0: WebView?, p1: String?, p2: String?, p3: String?, p4: JsPromptResult?): Boolean {
            return true
        }

        override fun onJsBeforeUnload(p0: WebView?, p1: String?, p2: String?, p3: JsResult?): Boolean {
            return true
        }

        //获取H5页面中控制台打印信息
        override fun onConsoleMessage(console: ConsoleMessage): Boolean {
            Log.i("X5WebChromeClient", "onConsoleMessage: ${console.message()}")
            return true
        }

    }

}